
from django.contrib.auth.models import User
from django.db import models
from utils.modelsMinxin import DateTimeModelsMixin, ModelsSetMixin


# Create your models here.

# 标签模型
class Label(DateTimeModelsMixin):
    name = models.CharField(max_length=30,verbose_name="标签名",unique=True)
    class Meta:
        db_table = 'Label'

    def __str__(self):
        return self.name

# 文章模型
class Article(ModelsSetMixin):
    STATUS_CHOICES = ((0,"未发布"),(1,"发布"))
    title = models.CharField(max_length=100,verbose_name="标题")
    digest = models.CharField(max_length=300,verbose_name="摘要")
    content = models.TextField(verbose_name="文章内容")
    page_view = models.IntegerField(verbose_name="浏览量",default=0)
    priority = models.IntegerField(verbose_name="优先级",default=0)
    status = models.IntegerField(choices=STATUS_CHOICES,default=0,verbose_name="状态")
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    label = models.ForeignKey(Label,on_delete=models.CASCADE)
    class Meta:
        db_table = 'Article'
        ordering = ['-priority',"-page_view","-create_time"]
    def __str__(self):
            return "标题:%s,作者:%s" % (self.title, self.user.username)

# 评论模型

class Comment(ModelsSetMixin):
    content = models.TextField(verbose_name="评论内容")
    article = models.ForeignKey(Article,on_delete=models.CASCADE)
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    # 关联自身
    parent_comment = models.ForeignKey("self",verbose_name="父级评论id",null=True,blank=True,on_delete=models.CASCADE,related_name="parent_comment_set")

    # 回复的哪一条评论
    reply_comment = models.ForeignKey("self",verbose_name="回复评论",null=True,blank=True,on_delete=models.CASCADE,related_name="reply_comment_set")
    class Meta:
        ordering = ['-create_time']
        db_table = 'Comment'

    def __str__(self):
        return "%s:%s" % (self.user.username, self.content)